package com.qiantu.phone.widget.setting;

import android.app.Activity;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.hjq.base.BaseActivity;
import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.BuildConfig;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.api.utils.ThemeUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.event.DeviceItemChangeEvent;
import com.qiantu.phone.event.DeviceDetailChangeEvent;
import com.qiantu.phone.glide.GlideApp;
import com.qiantu.phone.ui.activity.DeviceIconSelectActivity;

import org.greenrobot.eventbus.EventBus;

import okhttp3.Call;

public class DeviceSettingIcon extends LinearLayout {
    private AppActivity mContext;
    private DeviceBean mDevice;
    TextView textView;
    ImageView iconDevice;

    public DeviceSettingIcon(AppActivity context, DeviceBean bean) {
        this(context, bean, null);
    }

    public DeviceSettingIcon(AppActivity context, DeviceBean bean, @Nullable AttributeSet attrs) {
        this(context, bean, attrs, 0);
    }

    public DeviceSettingIcon(AppActivity context, DeviceBean bean, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, bean, attrs, defStyleAttr, 0);
    }

    public DeviceSettingIcon(AppActivity context, DeviceBean bean, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        mDevice = bean;
        LayoutInflater.from(getContext()).inflate(R.layout.device_setting_icon, this, true);
        setBackgroundResource(R.drawable.setting_item_pressed_bg);
        if(mDevice.getDeviceGroup() == 1){
            textView = findViewById(R.id.text_device);
            textView.setText(R.string.device_group_icon);
        }
        iconDevice = findViewById(R.id.icon_device);
        String imageUrl = BuildConfig.APP_RESOURCE_BASE + ThemeUtil.getIconThemeSuffix() + mDevice.getImageUrl() + "_on_home" +".png";
        GlideApp.with(iconDevice).load(imageUrl).into(iconDevice);
        int dp_16 = getResources().getDimensionPixelSize(R.dimen.dp_16);
        int dp_14 = getResources().getDimensionPixelSize(R.dimen.dp_14);
        setPadding(dp_16, dp_14, dp_16, dp_14);
        setGravity(Gravity.CENTER_VERTICAL);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, DeviceIconSelectActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                intent.putExtra("type", mDevice.getType());
                intent.putExtra("deviceType", mDevice.getDeviceType());
                intent.putExtra("selectIcon", mDevice.getImageUrl());
                mContext.startActivityForResult(intent, new BaseActivity.OnActivityCallback() {
                    @Override
                    public void onActivityResult(int resultCode, Intent data) {
                        if (resultCode == Activity.RESULT_OK) {
                            String selectIconUrl = data.getStringExtra("DATA");
                            mDevice.setImageUrl(selectIconUrl);
                            modifyDeviceIcon();
                        }
                    }
                });
            }
        });
    }

    private void modifyDeviceIcon() {
        HttpCallback<HttpData<Void>> callback = new HttpCallback<HttpData<Void>>(mContext) {
            @Override
            public void onStart(Call call) {
                mContext.showDialog(mContext.getString(R.string.modifying));
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                String imageUrl = BuildConfig.APP_RESOURCE_BASE + ThemeUtil.getIconThemeSuffix() + mDevice.getImageUrl() + "_on_home" +".png";
                GlideApp.with(iconDevice).load(imageUrl).into(iconDevice);
                LLDeviceDBManager.getInstance(getContext()).put(mDevice);
                EventBus.getDefault().post(new DeviceItemChangeEvent(mDevice.getRoomSerialNo(), mDevice.getDeviceSerialNo(), mDevice.getIsFavourite()));
                EventBus.getDefault().post(new DeviceDetailChangeEvent(mDevice.getDeviceSerialNo()));
                mContext.toast(R.string.reset_success);
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                mContext.hideDialog();
            }
        };
        if(mDevice.getDeviceGroup() == 0){
            LLHttpManager.modifyDevice(mContext, mDevice, callback);
        }else {
            LLHttpManager.modifyDeviceGroup(mContext, mDevice, callback);
        }
    }
}
